var time = require("../../../utils/util.js")
var proportion = 0;
var maxmoney = [];
var flagTime = 0;
var max = 10;
var Imgs = [];
var index;
var voiceTime ;
var voiceurl;
var sendListen=0;
var imgurl = [];
var nowmoney = 10;
var id;
var answerId;
var wxNum;
var uuid;
var Index;
var figureTime;
var give = 0;                 //最终给的                
for (var i = 0; i <= max; i++) {
  maxmoney.push(i);
}
Page({
  data: {
    NoshowQue: 'display',
    showQue: 'none',
    question: "",
    introduce: "是临沂市第四人民医院副院长，临沂市网络成瘾戒治中心（简称临沂网戒中心）主任",
    givemoney: "￥0",
    answers: [],
    wait: "",
    maxmoney: maxmoney,
    nowmoney: max,
    choosemoney: "none",
    userImg: "",
    ImgNum: "",
    isSolved: "",
    showquestion: "",
    imgDisplay:"display",
    zan:"zan",
    zanImg:"../../image/zan.png",
    niming:"匿名用户"
    




  },
  addObject: function (a, b) {
    a = JSON.stringify(a);
    b = JSON.stringify(b);
    a = a.concat('。');
    a = a.replace('}。', "");
    b = b.replace('{', ",");
    a = a.concat(b);
    a = JSON.parse(a);
    return a;
  },
  toExpert:function(e){
    var expert=e.currentTarget.id;
    expert=this.data.answers[expert].master.id;
    wx.navigateTo({
      url: '../../experts/information/information?id='+expert,
    })
  },
  listenNum:function(answerId){
    sendListen=1;
    wx.request({
      url: 'https://www.liuxuanlong.xyz/fenda/wx/listen',
      data:{
          answerId:answerId
      },
      method:'GET',
      success:function(){

      },
    })
  },
  listen: function (e) {
    
    index = e.currentTarget.id;
    Index = index;
    var answer = this.data.answers;
    console.log(answer[index]);
    if(sendListen==0)
    this.listenNum(answer[index].id);
    voiceTime = answer[index].recordTime;
    var url = this.data.answers[index].recordUrl;
    
    var that = this;
    var font = answer[index].f5;
    console.log(voiceTime);


    for (var i = 0; i < answer.length; i++) {
      if (i != index) {
        answer[i].f5 = "点击播放";
        that.setData(
          {
            answers: answer
          }
        );
      }
    }
    if (font == "点击播放")
     {
      answer[index].f5='正在缓冲...';
      that.setData(
        {
          answers:answer
        }
      ); 
      wx.downloadFile({
        url: url,
        success: function (res) {
          voiceurl = res.tempFilePath;
          figureTime = setInterval(that.figureTime, 500);
          answer[index].f5 = "播放中...";
          that.setData(
            {
              answers: answer
            }
          );
          wx.playVoice({
            filePath: voiceurl,
            success:function(){
              
            },
          })
         
        }
      })
  }
    if (font == "播放中...") {
      wx.stopVoice();
      clearInterval(figureTime);
      wx.removeSavedFile({
        filePath: voiceurl,
      })
      flagTime=0;
      answer[index].f5 = "点击播放";
      that.setData(
        {
          answers: answer
        }
      );
    }
   /* if (font == "已暂停播放") {
      wx.playVoice({
        filePath: voiceurl,
      })
      figureTime = setInterval(that.figureTime, 500);
      answer[index].f5 = "播放中...";
      that.setData(
        {
          answers: answer
        }
      );
    }*/
  },
  figureTime: function () {
    flagTime = flagTime + 0.5;
    console.log(flagTime);
    if (flagTime >= voiceTime) {
      this.data.answers[Index].f5 = "点击播放";
      flagTime = 0;
      clearInterval(figureTime);
      this.setData(
        {
          answers: this.data.answers
        }
      );
    }
  },
  prewImg: function (e) {
    /*var index1 = e.currentTarget.id;
    var index2=e.target.dataset.index;
    
    imgurl.push(this.data.answers[index1].answerImgSet[index2].imgUrl);
    for (var i = 0; i < this.data.answers[index1].answerImgSet.length; i++) {
      if(i!=index2)
      imgurl.push(this.data.answers[index1].answerImgSet[i].imgUrl);
    }
    wx.previewImage({
      urls: imgurl,
    })
    imgurl = []*/
    wx.showToast({
      title: '仅供本人查看',
      icon:'loading',
      duration:1000
    })

  },
  cancelmoney: function () {
    this.setData(
      {
        choosemoney: "none"
      }
    );
  },
  confirmmoney: function () {
    var that = this;
    var times = Date();
    
    this.setData(
      {
        choosemoney: "none"
      }
    );
    wx.request({
      url: "",
      data: {
        //answerId:,      答案的ID
        //money:,         钱数
        //getDate:        得到的时间

      },
      success: function () {
        //提示给钱成功
      },
      fail: function () {
        //提示给钱失败
      },
    })
  },
  givemoney: function () {
    this.setData(
      {
        choosemoney: "display"
      }
    );
  },
  choosemoney: function (e) {
    var money = e.detail.value;
    money = maxmoney[money];
    give = money;
    this.setData(
      {
        nowmoney: max - give
      }
    );
  },
  addZan:function(wxNum){
    /*console.log("点赞");
     this.data.answers[index].praiseUserSet.length++;
     
    
    this.setData(
      {
        answers:this.data.answers,
        zan: ""
        
      }
    );*/
    console.log(answerId,wxNum)
    wx.request({
      url: "https://www.liuxuanlong.xyz/fenda/wx/praise",
      data:{
          answerId:answerId,
          wxNumber:wxNum
      },
      success:function(){

      },
    })
  },
  exmine: function () {
      wx.showToast({
        title: '仅供本人查看',
        icon:'loading',
        duration:1000
      })
  },
  zan: function (e) {
    index=e.currentTarget.id;
    this.data.answers[index].praiseImg1=this.data.answers[index].praiseImg2;
    this.data.answers[index].zanNum++;
    this.data.answers[index].zan="";
    this.setData(
      {
        answers:this.data.answers,
        zan:""
      }
    );
    answerId=this.data.answers[index].id;
    
    this.Login(); 
  },
  Login: function () {
    console.log("执行了")
    var that = this;

    wx.checkSession({
      success: function () {
        wx.getStorage({
          key: 'login_uuId',
          success: function (res) {

            wxNum = res.data;
            wx.request({
              url: "https://www.liuxuanlong.xyz/fenda/wx/checkLoginTime",
              data: {
                login_uuId: wxNum
              },
              success: function (res) {
                if (res.data == 'error') {
                  that.tologin();
                }
                else {
                  console.log(wxNum)
                  that.addZan(wxNum)
                }
              },
            })

          },
        })

      },
      fail: function () {
        console.log("fail")
        that.tologin();
      },

    })


    //

  },

  tologin: function () {
    var that = this;
    wx.login({
      success: function (res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: 'https://www.liuxuanlong.xyz/fenda/wx/wxLogin',
            data: {
              code: res.code
            },
            success: function (res) {
              console.log("return" + res.data);
              wx.setStorage({
                key: "login_uuId",
                data: res.data
              });

              wxNum = res.data;
              that.addZan(wxNum);


            }
          })
        } else {
          console.log('获取用户登录态失败！' + res.avatarUrl)
        }
      }
    })
  },
  have_listen:function(){
    var datas=this.data.answers;
    var answerId=[];
    for(var i=0;i<datas.length;i++)
    answerId.push(datas[i].id);
    wx.request({
      url: 'https://www.liuxuanlong.xyz/fenda/wx/listen',
      data:{
        answerId:answerId
      },
      success:function(e){
        
      },
    })
    
  },
  Loadquestion: function (uuid) {
    var that = this;
    console.log("请求")
    wx.request({
      url: "https://www.liuxuanlong.xyz/fenda/wx/findQuestionByIdOrUuName",
      data: {

        id: uuid,

      },
      method: "GET",
      success: function (res) {   //获取问题
        console.log(res.data,'成功')
        if(res.data.isAnonymous!='true')
        that.setData(
          {
            niming:res.data.isAnonymous
          }
        );
        Imgs = res.data.questionImgUrl;
        if(Imgs.length==0){
          that.setData(
            {
              imgDisplay:"none"
            }
          );
        }
        else{
          that.setData(
            {
              imgDisplay:"display"
            }
          );
        }
        if (res.data.isSolved == '是') {
          res.data.isSolved = "已解答";
          that.setData(
            {
              wait: "none",
              showquestion: "display"

            }
          );
        }
        else if(res.data.isSolved!='已关闭') {
          res.data.isSolved = "未解答";
          that.setData(
            {
              wait: "display",
              showquestion: "none",
              wait_text:"等待专家为您解答",
              wait_bgcolor:'white'
            }
          );
        }
        else{
          res.data.isSolved='已关闭';
          that.setData(
            {
              wait:"display",
              showquestion:"none",
              wait_text:"",
              wait_bgcolor:"gainsboro"
            }
          );
        }

        //关闭标志位

        for (var i = 0; i < res.data.answerList.length; i++) {
          res.data.answerList[i].praiseImg1='../../image/zan.png';
          res.data.answerList[i].praiseImg2='../../image/zan1.png';
          var zanFun={'zan':'zan'};
          var zanNum=res.data.answerList[i].praiseUserSet.length;
          var zanNum={zanNum:zanNum};
          res.data.answerList[i] = that.addObject(res.data.answerList[i], zanNum);
          res.data.answerList[i]=that.addObject(res.data.answerList[i],zanFun);
          
          var timeNow=Date.now();
          var time=res.data.answerList[i].answerTime;
          time=timeNow-time;
          time=parseInt(time/60000);
          if(time>60){
            if(time>1440)
            time=parseInt(time/1440)+'天';
            else
            time=parseInt(time/60)+'小时';
          }
          else{
            time=time+'分钟';
          }
          res.data.answerList[i].answerTime=time;
          
          res.data.answerList[i].f5 = "点击播放";
          if (res.data.answerList[i].recordUrl == "")
            res.data.answerList[i].recordDisplay = "none";
          else {
            res.data.answerList[i].recordDisplay = "display";
          }
        }
        for (var i = 0; i < res.data.answerList.length; i++) {
          if (res.data.answerList[i].info == "")
            res.data.answerList[i].textDisplay = "none";
          else {
            res.data.answerList[i].textDisplay = "display";
          }
        }
        /*for (var i = 0; i < res.data.answerList.length; i++) {
          if (res.data.answerList[i].answerImgSet.length == 0)
            res.data.answerList[i].imageDisplay = "none";
          else {
            res.data.answerList[i].imageDisplay = "display"
          }
        }*/
        that.setData(
          {
            question: res.data.info,
            userImg: res.data.questionImgUrl,
            ImgNum: res.data.questionImgUrl.length,
            answers: res.data.answerList,
            isSolved: res.data.isSolved,
            NoshowQue: 'none',
            showQue: 'display',
           
          }
        );
        that.have_listen();
        /*if (res.detail.answers == []) {
          that.setData(
            {
              wait: "display"
            }
          );
        }
        else {
          that.setData(
            {
              wait: "none"
            }
          );
        }*/
      },
      fail: function () {

      },
    })
    this.setData(
      {
        question: ""
      }
    );
  },
 
  Check: function () {
    //获取点赞人的微信号 检测是否为已点赞用户

  },
 
  onLoad: function (options) {
    // 生命周期函数--监听页面加载
      //this.CheckLogin(); 
   
      this.Loadquestion(options.id);
    
      
    this.Check();

  },

})